﻿using System;
using System.Collections.Generic;
using System.Text;
using eBlog.Cache;

namespace eBlog.Service
{
    public class CacheService<T>
    {
        static int _cacheSize = 50;
        static SmartCache<T> _cache = new SmartCache<T>(_cacheSize);

        CacheService()
        {
            _cacheSize = 50;
            _cache.CleanupInterval = TimeSpan.FromHours(24);
        }

        #region 添加缓存

        /// <summary>
        /// 添加一个缓存。
        /// </summary>
        /// <param name="T">缓存类型。</param>
        /// <param name="key"></param>
        public static void AddCache(T T,string key)
        {
            _cache.Add(key, T);
        }

        #endregion

        #region 删除缓存 or 缓存集合

        /// <summary>
        /// 删除缓存缓存
        /// </summary>
        /// <param name="key"></param>
        public static void RemoveCache(int key)
        {
            if (key > 0)
            {
                _cache.Remove(key.ToString());
            }

        }

        /// <summary>
        /// 删除缓存缓存集合
        /// </summary>
        /// <param name="keys"></param>
        public static void RemoveCache(IEnumerable<string> keys)
        {
            foreach (var key in keys)
            {
                _cache.Remove(key);
            }
        }

        /// <summary>
        /// 清除所有缓存
        /// </summary>
        public static void RemoveAllCache()
        {
            _cache.ClearCache();
        }

        #endregion

        #region 获取缓存

        /// <summary>
        /// 判断缓存在不在缓存中
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool IsCacheExist(string key)
        {
            T t;
            return _cache.TryGetValue(key, out t);
        }

        /// <summary>
        ///从缓存中获取
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetCacheItem(string key)
        {
            T t;
            if (_cache.TryGetValue(key, out t))
            {
                return t;
            }
            else
            {
                return t;
            }

        }
       
        public static void GetAll()
        {
            //这个方暂时还没写年
        }

        #endregion

        /// <summary>
        /// 获取缓存数量
        /// </summary>
        /// <returns></returns>
        public static int GetCacheCount()
        {
            return _cache.ItemCount;
        }
    }
}
